package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.eunm.HintEnums;
import com.ytjj.qmyx.admin.model.Permission;
import com.ytjj.qmyx.admin.model.Role;
import com.ytjj.qmyx.admin.model.request.RoleRequest;
import com.ytjj.qmyx.admin.model.request.UpdateButtonRequest;
import com.ytjj.qmyx.admin.model.request.UpdateMenuRequest;
import com.ytjj.qmyx.admin.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有平台角色
     * @return
     */
    @GetMapping(value = "/adminList")
    public CommonResult<List<Role>> adminList() {
        List<Role> roleList = roleService.adminList();
        return CommonResult.success(roleList);
    }

    /**
     * 查询所有店家角色
     * @return
     */
    @GetMapping(value = "/bankerList")
    public CommonResult<List<Role>> bankerList() {
        List<Role> roleList = roleService.bankerList();
        return CommonResult.success(roleList);
    }

    /**
     * 根据条件去查询平台角色
     * @param roleRequest
     * @return
     */
    @RequiresPermissions("role:list")
    @PostMapping(value = "/list")
    public CommonResult list(@RequestBody RoleRequest roleRequest) {
        List<Role> roleList = roleService.list(roleRequest);
        return CommonResult.success(CommonPage.restPage(roleList));
    }

    /**
     * 根据条件去查询店家子角色
     * @param roleRequest
     * @return
     */
    @PostMapping(value = "/bankerListPage")
    public CommonResult<CommonPage<Role>> bankerListPage(@RequestBody RoleRequest roleRequest) {
        List<Role> roleList = roleService.bankerListPage(roleRequest);
        return CommonResult.success(CommonPage.restPage(roleList));
    }

    /**
     * 创建平台角色
     * @param role
     * @return
     */
    @RequiresPermissions("role:create")
    @PostMapping(value = "/create")
    @OperLog(operModule = "角色管理模块", operDesc = "创建平台角色", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult create(@RequestBody Role role) {
        if (role.getRoleName() == null) {
            return CommonResult.failed(HintEnums.ROLE_NAME_IS_NOTNULL.getMessage());
        }
        //判断该部门下是否有相同角色名称
        List<Role> roles = roleService.selectByRoleName(role.getRoleName());
        if (!CollectionUtils.isEmpty(roles)){
            for (Role r : roles) {
                if (r.getDepartmentId().compareTo(role.getDepartmentId()) == 0) {
                    return CommonResult.failed(HintEnums.ROLE_NAME_IS_REPETITION.getMessage());
                }
            }
        }
        int count = roleService.create(role);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 创建店家角色
     * @param role
     * @return
     */
    @PostMapping(value = "/bankerCreate")
    @OperLog(operModule = "角色管理模块", operDesc = "创建店家角色", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult bankerCreate(@RequestBody Role role) {
        int count = roleService.bankerCreate(role);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequiresPermissions("role:update")
    @PostMapping(value = "/update")
    @OperLog(operModule = "角色管理模块", operDesc = "修改角色", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult update(@RequestBody Role role) {
        if (role.getRoleName() == null) {
            return CommonResult.failed(HintEnums.ROLE_NAME_IS_NOTNULL.getMessage());
        }
        //判断该部门下是否有相同角色名称
        List<Role> roles = roleService.selectByRoleName(role.getRoleName());
        if (!CollectionUtils.isEmpty(roles)){
            for (Role r : roles) {
                if (r.getDepartmentId().compareTo(role.getDepartmentId()) == 0 && r.getId().compareTo(role.getId()) != 0){
                    return CommonResult.failed(HintEnums.ROLE_NAME_IS_REPETITION.getMessage());
                }
            }
        }
        int count = roleService.update(role);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 停用/启用 角色
     * @param role
     * @return
     */
    /*@PostMapping(value = "/updateStatus")
    public CommonResult updateStatus(@RequestBody Role role) {
        int count = roleService.update(role);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }*/

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @RequiresPermissions("role:delete")
    @PostMapping(value = "/delete")
    @OperLog(operModule = "角色管理模块", operDesc = "批量删除角色", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult delete(@RequestParam("ids") List<Integer> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取角色对应的权限
     * @param roleId
     * @return
     */
    @GetMapping(value = "/permission")
    public CommonResult<List<Permission>> getPermissionList(@RequestParam Integer roleId) {
        List<Permission> permissionList = roleService.getPermissionListById(roleId);
        return CommonResult.success(permissionList);
    }

    /**
     * 根据角色修改权限
     * @param roleId
     * @param permissionIds
     * @return
     */
    @PostMapping(value = "/permission/update")
    @OperLog(operModule = "角色管理模块", operDesc = "根据角色修改权限", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updatePermission(@RequestParam Integer roleId,
                                         @RequestParam("permissionIds") List<Integer> permissionIds) {
        int count = roleService.updatePermission(roleId, permissionIds);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取角色对应的菜单
     * @param roleId
     * @return
     */
    @RequiresPermissions("role:menu")
    @GetMapping(value = "/menu")
    public CommonResult<Map<String,Object>> getMenuList(@RequestParam Integer roleId) {
        Map<String,Object> data = roleService.getMenuListById(roleId);
        log.debug("===查看权限roleId==={}", roleId);
        return CommonResult.success(data);
    }

    /**
     * 给角色分配菜单
     * @param updateMenuRequest
     * @return
     */
    @PostMapping(value = "/updateMenu")
    @OperLog(operModule = "角色管理模块", operDesc = "给角色分配菜单", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateMenu(@RequestBody UpdateMenuRequest updateMenuRequest) {
        int count = roleService.updateMenu(updateMenuRequest);
        return CommonResult.success(count);
    }

    /**
     * 获取角色对应的按钮
     * @param roleId
     * @return
     */
    @RequiresPermissions("role:buttonList")
    @GetMapping(value = "/buttonList")
    public CommonResult buttonList(@RequestParam Integer roleId) {
        List<Map<String,Object>> data = roleService.buttonList(roleId);
        return CommonResult.success(data);
    }

    /**
     * 给角色分配按钮
     * @param updateButtonRequest
     * @return
     */
    @PostMapping(value = "/updateButton")
    @OperLog(operModule = "角色管理模块", operDesc = "给角色分配按钮", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateButton(@RequestBody UpdateButtonRequest updateButtonRequest) {
        int count = roleService.updateButton(updateButtonRequest);
        return CommonResult.success(count);
    }

    /**
     * 角色逻辑删除
     */
    @GetMapping("/deleteIsExist")
    @OperLog(operModule = "角色管理模块",operDesc = "角色逻辑删除",operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult deleteIsExist(@RequestParam Integer id) {
        if (roleService.RoleList(id)>0) {
            return CommonResult.failed(HintEnums.DELETE_ROLE_FAIL.getMessage());
        }
        return CommonResult.success(roleService.deleteIsExist(id));
    }

    @GetMapping("/updateStatusById")
    public CommonResult updateStatusById(@RequestParam Integer id,@RequestParam Integer status) {
        Integer count = roleService.updateStatusById(id,status);
        if (1 == status){
            return CommonResult.success(count);
        }else if (0 == status){
            return CommonResult.success(count,"关闭后该角色下的账号将无法登录");
        }
        return CommonResult.failed("修改失败");
    }

    @GetMapping("/delete")
    public CommonResult delete(){
        return CommonResult.success(roleService.deleteAll());
    }
}
